package cn.mymall.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
 * @author zss
 * @Description
 * @create 2022-02-24 19:07
 */
@Component
public class GenericFastJson2JsonRedisSerializer implements RedisSerializer<Object> {
    static final byte[] EMPTY_ARRAY = new byte[0];
    static {
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
    }
    @Override
    public byte[] serialize(Object source) throws SerializationException {
        if (source == null) {
            return EMPTY_ARRAY;
        } else {
            try {
                return JSON.toJSONBytes(source, SerializerFeature.WriteClassName);
            } catch (Exception var3) {
                throw new SerializationException("Could not write JSON: " + var3.getMessage(), var3);
            }
        }
    }

    @Override
    public Object deserialize(byte[] source) throws SerializationException {
        if (isEmpty(source))
            return null;
        return JSON.parse(source);
    }

    private  boolean isEmpty(@Nullable byte[] data) {
        return data == null || data.length == 0;
    }
}
